package net.starwifi.duobao.web.db;

import net.starwifi.duobao.dto.DBOrderDTO;
import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/19.
 */
@Controller("apiDBProductController")
@RequestMapping("/db")
public class ProductController extends BaseController {

    @Autowired
    DBProductService dbProductService; //夺宝产品

    @Autowired
    DBTimesService dbTimesService; //历史期
    @Autowired
    DBCodeService dbCodeService;
    @Autowired
    DBOrderService dbOrderService;
    @Autowired
    DBShareService dbShareService;

    @Autowired
    MemberService memberService;


    /** 夺宝期订单情况(我的订单，times)*/
    @ResponseBody
    @RequestMapping(value = "/times-data", method = RequestMethod.GET)
    public Object getTimesOrder(String timesNo){
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        Map<String, Object> result=new HashMap<String, Object>();
        Member member=memberService.getCurrent();
//        if(member!=null){
//            List<DBOrder> myOrderList=dbOrderService.getListData("o.timesNo=?1 and o.paymentStatus=?2 and o.memberId=?3 and o.codeInfo!=''",new Object[]{timesNo,1,member.getId()},orderBy);
//            result.put("myOrder", myOrderList);
//        }

        if(member!=null){
            List<DBCode> myCodeList=dbCodeService.getListData("o.timesNo=?1 and o.memberId=?2",new Object[]{timesNo,member.getId()},orderBy);
            result.put("myCode", myCodeList);
        }
        List<DBOrder> orderList=dbOrderService.getListData("o.timesNo=?1 and o.paymentStatus=?2 and o.codeInfo!=''", new Object[]{timesNo, 1}, orderBy);
        result.put("order", DBOrderDTO.getList(orderList));
        return Message.createMessage(Message.success("success"),result);
    }

    /**
     * 中奖名单
     * @param q
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/times-lottery", method = RequestMethod.GET)
    public Object Lottery( String timesNo) {
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        Map<String, Object> result=new HashMap<String, Object>();
       DBTimes dbTimes= dbTimesService.findByTimesNo(timesNo);
        if(dbTimes.getIsLottery()==true&&dbTimes.getLotteryMobile()!=null){
           Member member= memberService.findByMobile(dbTimes.getLotteryMobile());
            if(member!=null){
                List<DBCode> lotteryCodeList=dbCodeService.getListData("o.timesNo=?1 and o.memberId=?2",new Object[]{timesNo,member.getId()},orderBy);
                result.put("lotteryCode", lotteryCodeList);
            }
        }

        return Message.createMessage(Message.success("success"), result);
    }



    /**
     * 夺宝进度
     */
    @ResponseBody
    @RequestMapping(value = "/times-cq", method = RequestMethod.GET)
    public Object cQ(String q) {
        Integer cq= dbTimesService.getCQ(q);
        return Message.success(cq.toString());
    }

    /**
     * 一元夺宝产品页
     */
    @RequestMapping(value = "/productS-{id}-{timesNo}", method = RequestMethod.GET)
    public String productS(Model model,@PathVariable Long id,@PathVariable String timesNo) {
        DBTimes dbTimes=dbTimesService.findByTimesNo(timesNo);
        if(dbTimes.getIsLottery()==true){

        }
        model.addAttribute("dbProduct",dbTimes.getDbProduct());
        model.addAttribute("dbTimes",dbTimes);
        return "/db/product_s";
    }

    /**
     * 商品图文详情
     */
    @RequestMapping(value = "/product-detail-{id}", method = RequestMethod.GET)
    public String productDetail(Model model,@PathVariable Long id) {
        model.addAttribute("dbProduct",dbProductService.find(id));
        return "/db/detail";
    }

    /**
     * 往期揭晓
     */
    @RequestMapping(value = "/product-allRecord-{id}", method = RequestMethod.GET)
    public String allRecord(Model model,@PathVariable Long id) {
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<DBTimes> dbTimesList=dbTimesService.getListData(0,20,"o.dbProduct.id=?1 and o.isOver=?2",new Object[]{id,Boolean.TRUE},orderBy);
        model.addAttribute("dbTimesList",dbTimesList);
        return "/db/allRecord";
    }



    /**
     * 晒单分享
     */
    @RequestMapping(value = "/shareRecord-{id}", method = RequestMethod.GET)
    public String shareRecord(Model model,@PathVariable Long id)
    {
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<DBShare> dbShareList=dbShareService.getListData(0,10,"o.productId=?1 and o.isShow=?2",new Object[]{id,Boolean.TRUE},orderBy);
        model.addAttribute("dbShareList",dbShareList);
        model.addAttribute("productId",id);
        return "/db/shareRecord";
    }

    @RequestMapping(value = "/shareRecord-content", method = RequestMethod.GET)
    public String dbTimesRecordContent(Model model, int pageNum,Long id) {
        Member member = memberService.getCurrent();
        PageView pageView = new PageView(pageNum, 10);
        pageView.addOrder(net.starwifi.duobao.mvc.Order.desc("id"));
        pageView.addFilter(Filter.eq("productId", id));
        pageView.addFilter(Filter.eq("isShow", Boolean.TRUE));
        Page<DBShare> page = dbShareService.getScrollData(pageView);
        model.addAttribute("dbTimesList", page.getContent());
        return "/db/share-content";
    }

}
